package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICrmOperationRecordDAO;
import org.jsola.hr.dto.CrmOperationRecordAddDTO;
import org.jsola.hr.dto.CrmOperationRecordUpdateDTO;
import org.jsola.hr.entity.CrmOperationRecordDO;
import org.jsola.hr.query.CrmOperationRecordQuery;
import org.jsola.hr.service.ICrmOperationRecordService;
import org.jsola.hr.vo.CrmOperationRecordListVO;
import org.jsola.hr.vo.CrmOperationRecordVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 操作记录表
 *
 * @author zhr
 */
@Service("hrCrmOperationRecordServiceImpl")
public class CrmOperationRecordServiceImpl implements ICrmOperationRecordService {

    @Autowired
    private ICrmOperationRecordDAO crmOperationRecordDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CrmOperationRecordVO save(CrmOperationRecordAddDTO crmOperationRecordAddDTO, TokenUser tokenUser) {
        // 转DO
        CrmOperationRecordDO crmOperationRecordDO = crmOperationRecordAddDTO.to(CrmOperationRecordDO.class);
        // 保存
        crmOperationRecordDO = save(crmOperationRecordDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return crmOperationRecordDO.to(CrmOperationRecordVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CrmOperationRecordUpdateDTO crmOperationRecordUpdateDTO, TokenUser tokenUser) {
        //转DO
        CrmOperationRecordDO crmOperationRecordDO = crmOperationRecordUpdateDTO.to(CrmOperationRecordDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(crmOperationRecordDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... crmOperationRecordIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) crmOperationRecordIds);
    }


    @Override
    public CrmOperationRecordVO selectById(String crmOperationRecordId, String siteId) {
        CrmOperationRecordDO crmOperationRecordDO = selectDOById(crmOperationRecordId, siteId);
        if (crmOperationRecordDO == null) {
            return null;
        }
        return crmOperationRecordDO.to(CrmOperationRecordVO.class);
    }

    @Override
    public List<CrmOperationRecordListVO> select(CrmOperationRecordQuery crmOperationRecordQuery, String siteId) {
        List<CrmOperationRecordDO> crmOperationRecordDOList = selectDO(crmOperationRecordQuery, siteId);
        if(CollectionUtils.isEmpty(crmOperationRecordDOList)) {
            return crmOperationRecordDOList == null ? null : new ArrayList<>();
        }
        return crmOperationRecordDOList.stream()
                .map(crmOperationRecordDO -> crmOperationRecordDO.to(CrmOperationRecordListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CrmOperationRecordQuery crmOperationRecordQuery, String siteId) {
        Example example = buildExample(crmOperationRecordQuery,siteId);
        return crmOperationRecordDAO.selectCountByExample(example);
    }

    @Override
    public Page<CrmOperationRecordListVO> selectPage(CrmOperationRecordQuery crmOperationRecordQuery, String siteId) {
        Example example = buildExample(crmOperationRecordQuery,siteId);
        Page<CrmOperationRecordDO> page = crmOperationRecordDAO.selectPageByExample(example,
                crmOperationRecordQuery.getPageNo(),
                crmOperationRecordQuery.getPageSize());

        return page.to(CrmOperationRecordListVO.class);
    }

    @Override
    public CrmOperationRecordDO selectDOById(String crmOperationRecordId, String siteId) {
        return listById(crmOperationRecordId, siteId);
    }

    @Override
    public List<CrmOperationRecordDO> selectDO(CrmOperationRecordQuery crmOperationRecordQuery, String siteId) {
        Example example = buildExample(crmOperationRecordQuery,siteId);
        return crmOperationRecordDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param crmOperationRecordQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(CrmOperationRecordQuery crmOperationRecordQuery, String siteId) {
        Example example = new Example(CrmOperationRecordDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,crmOperationRecordQuery.getOrders());
        return example;
    }
}




